//
//  StorageUtil.m
//  寻TA
//
//  Created by 众里寻TA on 15-2-8.
//  Copyright (c) 2015年 Ta. All rights reserved.
//

#import "StorageUtil.h"
#import <objc/runtime.h>

@implementation StorageUtil
#pragma mark - Custom Method

//以下为文件读写操作
//读取本地数据
+(NSDictionary *) readFile:(NSString *)fileName
{
    NSString* Path =[self formatPath:fileName];
    NSDictionary *result = [[NSDictionary alloc]initWithContentsOfFile:Path];
    return result;
}

//读取本地数据
+(NSMutableArray *) readFileMutableArray:(NSString *)fileName
{
    NSString* Path =[self formatPath:fileName];
    NSMutableArray *arr=[NSMutableArray arrayWithContentsOfFile:Path];
    return arr;
}

+ (NSString *) readFileStr:(NSString *)fileName
{
    NSString* Path =[self formatPath:fileName];
    NSString *str= [NSString stringWithContentsOfFile:Path encoding:NSUTF8StringEncoding error:nil];
    return str;
}

//写入内容到本地
+ (void) writeToFile:(NSString *)fileName data:(NSString*)data
{
    NSString* Path =[self formatPath:fileName];
    NSError *error = nil;
    BOOL isTrue = [data writeToFile:Path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (!isTrue) {
    }else{
    }
}

//写入内容到本地
+ (void) writeImageToFile:(NSString *)fileName data:(NSData*)data
{
    NSString* Path =[self formatPath:fileName];
    BOOL isTrue = [data writeToFile:Path atomically:YES];
    if (!isTrue) {
    }else{
    }
}

//组装文件路径
+(NSString *) formatPath:(NSString *)fileName
{
//    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString* Path = [documentDirectory stringByAppendingPathComponent:fileName];
    return Path;
}
@end
